package com.wxy.gxs.springbootprojectday25.controller;

import com.wxy.gxs.springbootprojectday25.bean.Type;
import com.wxy.gxs.springbootprojectday25.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping("/type")
public class TypeControlller {
    @Autowired
    private TypeService typeService;

    @RequestMapping("/listType")
    public String findAll(@PageableDefault(size = 3,sort = {"id"},direction = Sort.Direction.ASC)Pageable pageable , Model model){

        Page<Type> types = typeService.listType(pageable);
        model.addAttribute("pages",types);
        return "typeList";

    }
    @RequestMapping("/toAdd")
    public String toAdd(){
        return "/add";

    }
    @RequestMapping("/add")
    public String add(Type type, RedirectAttributes attributes){
        Type type1= typeService.add(type);
        if(type1!=null){
            attributes.addFlashAttribute("message","添加成功!");

        }else{
            attributes.addFlashAttribute("message","添加失败");
        }


        return "redirect:/type/listType";

    }
//    修改
    @RequestMapping("/toModify/{id}")
    public  String modify(@PathVariable("id") Long id) {
        System.out.println("获取到的编号"+id);


        return "add";
    }


}
